import { createStore, createLogger } from 'vuex'
import axios from 'axios'

interface SubListItemState {
  title: string,
  id: string,
  desc: string,
  price: number
}

export interface ListItemState {
  images?: [string, string, string],
  rightImg?: string,
  vedio?: string,

  title: string,
  id: string,
  desc: string,
  createDate: string,
  banner: string[],
  subList: SubListItemState[],
  descImg: {
    face: string[],
    trim: string[],
    space: string[],
  }
}




export interface RootState {
  list: ListItemState[]
}

export default createStore<RootState>({
  state: {
    list: []
  },
  mutations: {
    SET_LIST(state, payload) {
      state.list = [...state.list, ...payload]
    }
  },
  actions: {
    fetchList(context, params) {
      return new Promise((resolve, reject) => {
        axios.get('/api/list', { params }).then(resp => {
          if (resp.data.code === 200) {
            context.commit('SET_LIST', resp.data.data);
            resolve(resp.data)
          } else {
            reject(resp.data.message)
          }
        })
      })
    }
  },
  modules: {
  },
  plugins: [createLogger()]
})
